#!/bin/env ruby
# encoding: utf-8
require_relative '../spec/spec_helper'
require_relative '../spec/commons/user_auth'
require_relative '../spec/commons/check_pages'

describe 'Profile Page' do
  before {
    
  }



  context "when logged in" do
    it "then he can edit data in Profile"  do
      goto 'http://pulse.7bits.it/en/login-page'
      sign_in('employee.d@mail.ru', '123qwe+')
      check_profile_page()      
    end
  end

# - first name: 
#  -- submit empty input:  “First name cannot be empty”
  context 'when submit first name' do
    it 'then first name cannot be empty' do
      old_text = text_field(id: "firstName", class: "form-control", name:"firstName").text
      text_field(id: "firstName", class: "form-control", name:"firstName").clear
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'First name cannot be empty').wait_until_present(10)
      text_field(id: "firstName", class: "form-control", name:"firstName").set old_text
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
    end
  end

#  -- submit string with length more than 255:  “First name should contain no more than 255 symbols”
  context 'when submit first name' do
    it 'then it should contain no more than 255 symbols' do
      old_text = text_field(id: "firstName", class: "form-control", name:"firstName").text
      text_field(id: "firstName", class: "form-control", name:"firstName").clear
      ustr='a'
      256.times{|i| ustr+='a'}
      text_field(id: 'firstName', class: 'form-control', name: 'firstName').set ustr
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'First name should contain no more than 255 symbols').wait_until_present(10)
      text_field(id: "firstName", class: "form-control", name:"firstName").set old_text
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
    end
  end

#  - middle name: 
#  -- submit string with length more than 255:  “Middle name should contain no more than 255 symbols”
  context 'when submit middle name' do
    it 'then middle name cannot be empty' do
      old_text = text_field(id: "middleName", class: "form-control", name:"middleName").text
      text_field(id: "middleName", class: "form-control", name:"middleName").clear
      ustr='a'
      256.times{|i| ustr+='a'}
      text_field(id: 'middleName', class: 'form-control', name: 'middleName').set ustr
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Middle name should contain no more than 255 symbols').wait_until_present(10)
      text_field(id: "middleName", class: "form-control", name:"middleName").set old_text
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
    end
  end


#  - last name: 
#  -- submit empty input:  “Last name cannot be empty”
  context 'when submit last name' do
    it 'then last name cannot be empty' do
      old_text = text_field(id: "lastName", class: "form-control", name:"lastName").text
      text_field(id: "lastName", class: "form-control", name:"lastName").clear
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Last name cannot be empty').wait_until_present(10)
      text_field(id: "lastName", class: "form-control", name:"lastName").set old_text
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
    end
  end

#  -- submit string with length more than 255:  “Last name should contain no more than 255 symbols”
  context 'when submit last name' do
    it 'then last name cannot be empty' do
      old_text = text_field(id: "lastName", class: "form-control", name:"lastName").text
      text_field(id: "lastName", class: "form-control", name:"lastName").clear
      ustr='a'
      256.times{|i| ustr+='a'}
      text_field(id: 'lastName', class: 'form-control', name: 'lastName').set ustr
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Last name should contain no more than 255 symbols').wait_until_present(10)
      text_field(id: "lastName", class: "form-control", name:"lastName").set old_text
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
    end
  end

#  - old password (only if new password is filled):
#  -- submit empty input:  “Please fill out old password”
  context 'when submit old password' do
    it 'then it cannot be empty, if new password is not empty' do
      text_field(id: "newPassword", class: "form-control", type: "password", name: "newPassword").set '12345678'
      text_field(id: "oldPassword", class: "form-control", type: "password", name: "oldPassword").clear
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Please fill out old password').wait_until_present(10)
    end
  end
  

#  -- submit wrong old password: “Old password is incorrect” 
  context 'when submit old password' do
    it 'then it should be correct' do
      text_field(id: "newPassword", class: "form-control", type: "password", name: "newPassword").set '12345678'
      text_field(id: "oldPassword", class: "form-control", type: "password", name: "oldPassword").set '123qwee'
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Old password is incorrect').wait_until_present(10)
      end
    end

#  - new password:
#  -- submit string with length more than 255 symbols:  “Password should contain no more than 255 symbols”
  context 'when submit new password' do
    it 'then should contain no more than 255 symbols' do
      ustr='a'
      256.times{|i| ustr+='a'}
      text_field(id: "newPassword", class: "form-control", type: "password", name: "newPassword").set ustr
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Password should contain no more than 255 symbols').wait_until_present(10)
    end
  end

#  -- submit string with length less than 5 symbols: “Password should be longer than 4 symbols”
  context 'when submit new password' do
    it 'then should be longer than 4 symbols' do     
      text_field(id: "oldPassword", class: "form-control", type: "password", name: "oldPassword").set '123qwe+'
      text_field(id: "newPassword", class: "form-control", type: "password", name: "newPassword").set '123'
      button(class: "btn btn-success js-submit-btn", value: "Save", name: "submit").click
      span(class: "help-block",text: 'Password should be longer than 4 symbols').wait_until_present(10)
    end
  end




  context "when logged out" do
    it "then he can't edit data in Profile"  do
      user_logout()
    end
  end
 
end